package com.eis.admin.mvc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.eis.admin.mvc.bean.BizGroupBean;
import com.eis.admin.mvc.bean.FunctionBean;
import com.eis.admin.mvc.common.utils.MVCConstants;
import com.eis.admin.mvc.controller.base.MVCBaseController;
import com.eis.persist.dao.UserDAO;
import com.eis.persist.dao.UserFunctionDAO;
import com.eis.persist.dao.impl.UserDAOImpl;
import com.eis.persist.dao.impl.UserFunctionDAOImpl;
import com.eis.persist.model.user.PersistBizGroup;
import com.eis.persist.model.user.PersistUser;
import com.eis.util.XmlUtil;

@Controller
public class BizGroupAdminController extends MVCBaseController {
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(BizGroupAdminController.class);
	private UserDAO userDAO;
	@SuppressWarnings("unused")
	private XmlUtil xmlUtil;
	public void setRuleUtil(XmlUtil xmlUtil) {
		this.xmlUtil = xmlUtil;
	}

	@Override
	public void setFunctionDataSource(JdbcTemplate functionDataSource) {
		this.functionDataSource = functionDataSource;
		userDAO = new UserDAOImpl(this.functionDataSource);
	}
	
	@RequestMapping(value = "/bizgroupIntroduction")
	public ModelAndView bizgroupIntroduction(Model model, HttpServletRequest request) {
		return new ModelAndView("/bizgroup/bizgroupIntroduction");
	}
	
	@RequestMapping(value = "/bizgroupHome")
	public ModelAndView bizgroupHome(@ModelAttribute("searchBizGroup") BizGroupBean bizGroup, Model model, HttpServletRequest request) {
		BizGroupBean filter = null;
		if (bizGroup != null && bizGroup.isFilterable()) {
			filter = bizGroup;
		}
		
		PersistUser currentUser = super.getCurrentUser(request);
		
		List<BizGroupBean> bizGroups = userDAO.getBizGroupForList(filter, currentUser.getBizGroup().getId());

		model.addAttribute("bizGroupList", bizGroups);

		return new ModelAndView("/bizgroup/bizgroupHome", "command", bizGroup);
	}
	
	
	@RequestMapping(value = "/customizeActionerLayout")
	public ModelAndView customizeActionerLayout(@ModelAttribute("searchBizGroup") BizGroupBean bizGroup, Model model, HttpServletRequest request) {
		BizGroupBean filter = null;
		if (bizGroup != null && bizGroup.isFilterable()) {
			filter = bizGroup;
		}
		
		PersistUser currentUser = super.getCurrentUser(request);
		
		List<BizGroupBean> bizGroups = userDAO.getBizGroupForList(filter, currentUser.getBizGroup().getId());

		model.addAttribute("bizGroupList", bizGroups);

		return new ModelAndView("/function/customizeActionerLayout", "command", bizGroup);
	}
	
	@RequestMapping(value = "/assignFunctions")
	public ModelAndView assignFunctions(@ModelAttribute("searchFunction") FunctionBean function, Model model, HttpServletRequest request) {
		// get bizsysid from request
		int bizGroupId = 0;
		try {
			bizGroupId = Integer.valueOf(request.getParameter("bizGroupId"));
		} catch (Throwable t) {
		}
		
		PersistUser currentUser = super.getCurrentUser(request);
		if (bizGroupId != 0) {
			// load business group
			PersistBizGroup persistBizGroup = userDAO.getBizGroupById(bizGroupId);
			if (persistBizGroup != null) {
				BizGroupBean bizGroupBean = new BizGroupBean(persistBizGroup);
				
				FunctionBean filter = null;
				if (function != null && function.isFilterable()) {
					filter = function;
				}
				
				UserFunctionDAO userFunctionDAO = new UserFunctionDAOImpl(this.functionDataSource);

				List<FunctionBean> functions = userFunctionDAO.getFunctionsForBizgroupAssign(currentUser.getBizGroup().getId(), bizGroupId, filter);

				if (functions == null) {
					setWarningMessages(request, "You cannot operate this business group, please choose another one.!");
				} else {
					model.addAttribute("functionList", functions);

					return new ModelAndView("/bizgroup/assignFunctions", "bizGroupBean", bizGroupBean);
				}
			}
		}
		
		List<BizGroupBean> bizGroups = userDAO.getBizGroupForList(new BizGroupBean(), currentUser.getBizGroup().getId());

		model.addAttribute("bizGroupList", bizGroups);

		model.addAttribute("targetpath", "assignFunctions");

		return new ModelAndView("/bizgroup/selectBizgroup");
	}
	
	@RequestMapping(value = "/removeFunction")
	public ModelAndView removeFunction(Model model, HttpServletRequest request) {
		// get bizGroupId, functionId from request
		PersistUser currentUser = super.getCurrentUser(request);
		int bizGroupId = 0;
		String functionId = "";
		try {
			bizGroupId = Integer.valueOf(request.getParameter("bizGroupId"));
			functionId = request.getParameter("functionId");
		} catch (Throwable t) {
		}
		
		// PersistUser currentUser = super.getCurrentUser(request);
		boolean operationResult = false;
		if (bizGroupId != 0 && functionId.length() > 0) {
			UserFunctionDAO userFunctionDAO = new UserFunctionDAOImpl(this.functionDataSource, currentUser.getName());
			operationResult = userFunctionDAO.removeFunction(bizGroupId, functionId);
		}

		if (operationResult) {
			return new ModelAndView("redirect:" + appPrefix + "/assignFunctions?bizGroupId=" + bizGroupId, prepareRedirectWarnMessage("function have been removed successfully!"));
		} else {
			return new ModelAndView("redirect:" + appPrefix + "/assignFunctions?bizGroupId=" + bizGroupId, 
					prepareRedirectErrorMessage("Your operation is fail!"));
		}
		
	}
	
	@RequestMapping(value = "/assignFunction")
	public ModelAndView assignFunction(@ModelAttribute("searchFunction") FunctionBean function, Model model, HttpServletRequest request) {
		// get bizGroupId, functionId from request
		PersistUser currentUser = super.getCurrentUser(request);
		int bizGroupId = 0;
		String functionId = "";
		try {
			bizGroupId = Integer.valueOf(request.getParameter("bizGroupId"));
			functionId = request.getParameter("functionId");
		} catch (Throwable t) {
		}

		// PersistUser currentUser = super.getCurrentUser(request);
		boolean operationResult = false;
		if (bizGroupId != 0 && functionId.length() > 0) {
			UserFunctionDAO userFunctionDAO = new UserFunctionDAOImpl(this.functionDataSource, currentUser.getName());
			operationResult = userFunctionDAO.assignFunction(bizGroupId, functionId);
		}

		if (operationResult) {
			return new ModelAndView(
					"redirect:" + appPrefix + "/assignFunctions?bizGroupId=" + bizGroupId,
					prepareRedirectWarnMessage("function have been assigned successfully!"));
		} else {
			return new ModelAndView("redirect:" + appPrefix
					+ "/assignFunctions?bizGroupId=" + bizGroupId,
					prepareRedirectErrorMessage("Your operation is fail!"));
		}
	}
	
	@RequestMapping(value = "/createBizGroup")
	public ModelAndView createBizGroup(Model model, HttpServletRequest request) {
		PersistUser currentUser = super.getCurrentUser(request);
		List<BizGroupBean> existGroups = userDAO.getBizGroupForList(null, currentUser.getBizGroup().getId());
		model.addAttribute("bizGroupList", existGroups);
		
		return new ModelAndView("/bizgroup/createBizGroup", "command", new BizGroupBean());
	}
	
	@RequestMapping(value = "/submitCreateBizGroup")
	public ModelAndView submitCreateBizGroup(Model model,
			@ModelAttribute("createBizGroup") BizGroupBean bizGroupBean,
			HttpServletRequest request) {
		PersistUser currentUser = (PersistUser) request.getSession().getAttribute(
				MVCConstants.USER_SESSION_KEY);
		UserDAO userDAO1 = new UserDAOImpl(this.functionDataSource, currentUser.getName());

		// TODO: validate
		
		userDAO1.createBizGroup(bizGroupBean);
		return new ModelAndView("redirect:" + appPrefix + "/bizgroupHome", prepareRedirectWarnMessage("Business Group '" 
				+ bizGroupBean.getName()
				+ "' have been created successfully!"));
	}
}
